public with sharing class ScheduledCargoPDFController {

	public String scid;
	public ScheduledCargo__c sc {get;set;}

    public List<List<SameBatchNumberBean>> sbList {get;set;}
    public Integer  pageCnt {get;set;}//页数
	public String sc_createddate {get;set;}
	public String company {get;set;}
    public List<String> reasons {get;set;}
    public Integer extroRow {get;set;}
    public List<String> extroRows {get;set;}

    public ScheduledCargoPDFController() {
        scid = System.currentPageReference().getParameters().get('id');
        sbList= new List<List<SameBatchNumberBean>>();
        reasons = new List<String>();
        extroRows = new List<String>();
        sc_createddate = '';
    }

    public void init(){

    	sc = [select Id,Name ,BuyReason__c ,WhetherHavePrice__c ,CreatedBy.Name ,CreatedDate, OwnerCompany__c, Customer__r.Name ,Reason__c from ScheduledCargo__c where id=:scid];
    	sc_createddate = sc.CreatedDate.format('yyyy年MM月dd日');


        reasons = autoNextLine(sc.Reason__c, 28);
        if(reasons.size() < 2 )reasons.add('');
        extroRow = reasons.size() > 2 ? reasons.size() - 2 : 0 ;
        for(Integer i = 0 ; i <extroRow ; i ++ ){
            extroRows.add('');
        }

       Map<String ,String> comMap = ImportObject.getPicklistValues('ScheduledCargo__c', 'OwnerCompany__c', true );
    	company = comMap.get(sc.OwnerCompany__c);

        List<SameBatchNumber__c> cpList = new List<SameBatchNumber__c> ();

    	for(SameBatchNumber__c s : [select BatchNumber__c,ExpirationDate__c,Id,Inventory__r.Name,Inventory__r.NCCode__c,Inventory__r.Spec__c,Inventory__r.Brand__c,
    		Inventory__r.Unit__c,Inventory__r.SerialNo__c,SendYear__c,SendMount1__c,SendMount2__c,SendMount3__c,SendMount4__c,OtherRequirements__c,Inventory__r.ProductName__c,
    		SendMount5__c,SendMount6__c,SendMount7__c,SendMount8__c,SendMount9__c,SendMount10__c,SendMount11__c,SendMount12__c

    	 from SameBatchNumber__c where CargoDetail__c =:scid]){
                cpList.add(s);
            
            
        }

        Integer rows = 0;
        Decimal totalrowHeight = 0;
         Integer idx = 0;
        List<SameBatchNumberBean>  temp = new List<SameBatchNumberBean>();

        for (SameBatchNumber__c cp :cpList) {
                //for (Integer i = 0; i < 10 ; i ++ ) {
                Decimal  h1 = 0;
                Decimal  h2 = 0;
                Decimal h3 = 0;
                if(cp.Inventory__r.Spec__c != null )  h1  = Math.ceil(cp.Inventory__r.Spec__c.length() / 7.0);
                if(cp.Inventory__r.ProductName__c  != null ) h2 = Math.ceil(cp.Inventory__r.ProductName__c.length() / 10.0);
                if(cp.Inventory__r.Brand__c != null ) h3 = Math.ceil(cp.Inventory__r.Brand__c.length() / 7.0);
                if(cp.OtherRequirements__c != null ) h3 = h3  <  Math.ceil(cp.OtherRequirements__c.length() / 5.0) ? Math.ceil(cp.OtherRequirements__c.length() / 5.0) : h3  ;

                 Decimal h = 0 ;
                h  =  h1 > h2  ? h1 : h2 ;
                h = h > h3 ? h : h3 ;

                Integer nowRows = rows ;
                rows += h > 2 ? Integer.valueOf(h) : 2 ;
                Decimal  rowHeight = h > 2 ? 22 +12*(h -2) : 22 ;
                totalrowHeight += rowHeight;
                temp.add(new SameBatchNumberBean(cp,idx+1,rowHeight));

                if((sbList.size() == 0 && totalrowHeight > (404 - extroRow * 22 ))|| totalrowHeight > 484 ){
                    System.debug(totalrowHeight);
                    Decimal maxH = sbList.size() == 0 ? (418- extroRow * 22 ) : 500 ;
                    List<SameBatchNumberBean> t = new List<SameBatchNumberBean>();
                    Decimal yHeight = 0;
                    if(totalrowHeight > maxH){

                      while(totalrowHeight > maxH){
                        SameBatchNumberBean rm = temp.get(temp.size() -1);

                        totalrowHeight -= rm.rowHeight;
                        yHeight += rm.rowHeight;
                        t.add(rm);
                        temp.remove(temp.size() -1);
                      }

                    }
                    List<SameBatchNumberBean> t2 = new List<SameBatchNumberBean>();
                          t2.addAll(temp);
                          sbList.add(t2);
                          temp.clear();
                          temp.addAll(t);
                          totalrowHeight = yHeight;
                }
                           
              idx ++;            
            
        }
        if(temp.size() >0)sbList.add(temp);
        pageCnt = sbList.size();


    }
    private List<String> autoNextLine(String s, Integer posLength ){
           List<String > temp= new List<String>();
          if(s == null || s.length() <=posLength ) 
          {
            temp.add(s);
            return temp; 
          }
          if(s.length() > posLength){
                 for(Integer i = 0; i < s.length(); i++){
                     if(Math.mod(i, posLength ) == 0){
                            if(s.length() > i + posLength){
                                 temp.add (s.substring(i, i + posLength) ); 
                            }else{ 
                                 temp.add (s.substring(i,s.length()));
                            } 
                      }
                 }
            }

              return temp;
        }

    public class SameBatchNumberBean{

    	public Integer indx {get;set;}
    	public String nccode {get;set;}
    	public String name {get;set;}
    	public String spec {get;set;}
    	public String brand {get;set;}
    	public String unit {get;set;}
    	public String serial {get;set;}
    	public String  amount {get;set;}
    	public String memo {get;set;}
        public String  invliddate  {get;set;}
        public String batachcode  {get;set;}


    	public String year {get;set;}
    	public String  a1 {get;set;}
    	public String a2 {get;set;}
		public String a3 {get;set;}
    	public String a4 {get;set;}
    	public String a5 {get;set;}
    	public String a6 {get;set;}
    	public String a7 {get;set;}
    	public String a8 {get;set;}
    	public String a9 {get;set;}
    	public String a10 {get;set;}
    	public String a11 {get;set;}
    	public String a12 {get;set;}
    	public Decimal rowHeight {get;set;} 

    	public SameBatchNumberBean(SameBatchNumber__c sn,Integer i,Decimal rh  ){
    		indx = i ;
            rowHeight = rh;
    		nccode = sn.Inventory__r.NCCode__c;
    		name = autoNextLine(sn.Inventory__r.ProductName__c ,10);
    		spec = autoNextLine(sn.Inventory__r.Spec__c,7);
    		brand = autoNextLine(sn.Inventory__r.Brand__c,7);
    		unit = sn.Inventory__r.Unit__c;
    		serial = sn.Inventory__r.SerialNo__c;
            batachcode = sn.BatchNumber__c;
            invliddate = sn.ExpirationDate__c == null ? '': Utils.date2string(sn.ExpirationDate__c, 'yyyy-MM-dd');

    		year = sn.SendYear__c;

    		amount = sumAmount(sn);
    		a1 = getStringValue(sn.SendMount1__c);
    		a2 = getStringValue(sn.SendMount2__c);
    		a3 = getStringValue(sn.SendMount3__c);
    		a4 = getStringValue(sn.SendMount4__c);
    		a5 = getStringValue(sn.SendMount5__c);
    		a6 = getStringValue(sn.SendMount6__c);
    		a7 = getStringValue(sn.SendMount7__c);
    		a8 = getStringValue(sn.SendMount8__c);
    		a9 = getStringValue(sn.SendMount9__c);
    		a10 = getStringValue(sn.SendMount10__c);
    		a11 = getStringValue(sn.SendMount11__c);
    		a12 = getStringValue(sn.SendMount12__c);
    		memo = autoNextLine(sn.OtherRequirements__c,5);


    	}

    	private String   sumAmount(SameBatchNumber__c sn  ){
    		Decimal	a1 = sn.SendMount1__c == null ? 0 : sn.SendMount1__c ;
    		Decimal	a2 = sn.SendMount2__c == null ? 0 : sn.SendMount2__c ;
    		Decimal a3 = sn.SendMount3__c == null ? 0 : sn.SendMount3__c ;

	    	Decimal	a4 = sn.SendMount4__c == null ? 0 : sn.SendMount4__c ;
	    	Decimal	a5 = sn.SendMount5__c == null ? 0 : sn.SendMount5__c ;
	    	Decimal	a6 = sn.SendMount6__c == null ? 0 : sn.SendMount6__c ;

	    	Decimal	a7 = sn.SendMount7__c == null ? 0 : sn.SendMount7__c ;
	    	Decimal	a8 = sn.SendMount8__c == null ? 0 : sn.SendMount8__c ;
	    	Decimal	a9 = sn.SendMount9__c == null ? 0 : sn.SendMount9__c ;

	    	Decimal	a11 = sn.SendMount11__c == null ? 0 : sn.SendMount11__c ;
	    	Decimal	a12 = sn.SendMount12__c == null ? 0 : sn.SendMount12__c ;
	    	Decimal	a10 = sn.SendMount10__c == null ? 0 : sn.SendMount10__c ;

    		return  String.valueOf(Integer.valueOf(a1 + a2 + a3 + a4 + a5 +a6 +a7 + a8 + a9 + a10 + a11 + a12));
    	}
    	private String getStringValue(Decimal d ){
    		return d == null ? '' : String.valueOf(Integer.valueOf(d)) ;
    	}
    	public SameBatchNumberBean(Integer idx){
      		indx  = idx ;
      	}
    	private String autoNextLine(String s, Integer posLength ){
          if(s == null || s.length() <=posLength ) 
          {
              return s; 
          }
           String temp = '';
                  if(s.length() > posLength){
                     for(Integer i = 0; i < s.length(); i++){
                     if(Math.mod(i, posLength ) == 0){
                           if(s.length() > i + posLength){
                             temp += s.substring(i, i + posLength) + '\n'; 
                         }else{ 
                             temp+= s.substring(i,s.length());
                          } 
                  }
                     }
                  }

              return temp;
    	}





    }

}